package com.woniuxy.controller;

import com.woniuxy.dao.PrescriptionDao;
import com.woniuxy.entity.*;
import com.woniuxy.service.InpatientDrugService;
import com.woniuxy.service.InpatientInspectService;
import com.woniuxy.service.InpatientOperationService;
import com.woniuxy.service.InpatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

@RestController
public class InpatientController {
    @Value("${inpatient.path}")
    private String imgPath;

    @Autowired
    InpatientService inpatientService;
    @Autowired
    PrescriptionDao prescriptionDao;
    @Autowired
    InpatientDrugService inpatientDrugService;
    @Autowired
    InpatientOperationService inpatientOperationService;
    @Autowired
    InpatientInspectService inpatientInspectService;

    //查询待办住院病人人数
    @GetMapping("/readyInpatient")
    public ResponseResult getReadyInpatientNum(){
        Prescription prescription = new Prescription();
        prescription.setPrescriptionStatus(2);
        int num = prescriptionDao.getPrescriptionNum(prescription);
        return new ResponseResult(200, "ok", num);
    }

    //查询所有待办住院的病人
    @GetMapping("/inpatient/{pageIndex}/{pageSize}")
    public ResponseResult getReadyInpatient(@PathVariable int pageIndex, @PathVariable int pageSize){
        PageBean<Patient> patients = inpatientService.getReadyInPatient(pageIndex, pageSize);
        return new ResponseResult(200, "ok", patients);
    }

    //分页查询住院病人
    @PostMapping("/inpatient/{pageIndex}/{pageSize}")
    public ResponseResult getInpatient(@PathVariable int pageIndex, @PathVariable int pageSize, @RequestBody Inpatient inpatient){
        PageBean<Inpatient> inpatients = inpatientService.getInpatients(inpatient, pageIndex, pageSize);
        ResponseResult responseResult = new ResponseResult(200, "ok", inpatients);
        return responseResult;
    }

    //入院手续，添加住院病人
    @PostMapping("/inpatient")
    public ResponseResult insertStudent(@RequestBody Inpatient inpatient) {
        inpatientService.addInpatient(inpatient);
        ResponseResult responseResult = new ResponseResult(200, "ok", null);
        return responseResult;
    }

    //查询病人药品
    @GetMapping("/inpatientDrug/{inpatientId}")
    public ResponseResult getInpatientDrug(@PathVariable int inpatientId){
        List<InpatientDrug> inpatientDrugs = inpatientDrugService.getInpatientDrugs(inpatientId);
        ResponseResult responseResult = new ResponseResult<>(200, "ok", inpatientDrugs);
        return responseResult;
    }

    //查询病人检查
    @GetMapping("/inpatientInspect/{inpatientId}")
    public ResponseResult getInpatientInspect(@PathVariable int inpatientId){
        List<InpatientInspect> inpatientInspects = inpatientInspectService.getInpatientInspect(inpatientId);
        ResponseResult responseResult = new ResponseResult<>(200, "ok", inpatientInspects);
        return responseResult;
    }

    //添加病人检查结果
    @PutMapping("/inpatientInspect")
    public ResponseResult addInspectResult(@RequestBody InpatientInspect inpatientInspect) {
        inpatientInspectService.updateInpatientInspectResult(inpatientInspect.getInpatientInspectId(), inpatientInspect.getInpatientInspectResult());
        return new ResponseResult(200, "ok", null);
    }

    //添加病人检查图片
    @PostMapping("/inpatientInspectImg")
    public ResponseResult addImg(MultipartFile file){
        //原始文件名
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //使用UUID重新生成文件名，方式重复造成覆盖
        String fileName = UUID.randomUUID().toString() + suffix;
        //创建一个目录对象
        File dir = new File(imgPath);
        if (!dir.exists()){
            dir.mkdirs();
        }
        try {
            file.transferTo(new File(imgPath + fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ResponseResult(200, "ok", fileName);
    }

    //下载病人检查图片
    @GetMapping("/getInspectImg")
    public void download(String name, HttpServletResponse response){
        try {
            //输入流 下载图片
            FileInputStream fileInputStream = new FileInputStream(new File(imgPath + name));
            //输出流 写入浏览器
            ServletOutputStream outputStream = response.getOutputStream();
            response.setContentType("image/jpeg");
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = fileInputStream.read(bytes)) != 1) {
                outputStream.write(bytes, 0, len);
                outputStream.flush();
            }
            //关闭资源
            outputStream.close();
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //检查图片存入数据库
    @PostMapping("/addImg/{imgName}/{inpatientInspectId}")
    public ResponseResult addImg(@PathVariable("imgName") String name, @PathVariable("inpatientInspectId") int id){
        inpatientInspectService.addInpatientInspectImg(id, name);
        return new ResponseResult<>(200, "ok", null);
    }

    //查询病人手术
    @GetMapping("/inpatientOperation/{inpatientId}")
    public ResponseResult getInpatientOperation(@PathVariable int inpatientId){
        List<InpatientOperation> inpatientOperations = inpatientOperationService.getInpatientOperations(inpatientId);
        ResponseResult responseResult = new ResponseResult<>(200, "ok", inpatientOperations);
        return responseResult;
    }

    //添加病人手术结果
    @PutMapping("/inpatientOperation")
    public ResponseResult addOperationResult(@RequestBody InpatientOperation inpatientOperation) {
        inpatientOperationService.updateOperationResult(inpatientOperation.getInpatientOperationId(), inpatientOperation.getInpatientOperationResult());
        return new ResponseResult(200, "ok", null);
    }

    //添加检查
    @PostMapping("/inpatientInspect")
    public ResponseResult addCure(@RequestBody InpatientInspect inpatientInspect){
        inpatientInspectService.insertInpatientInspect(inpatientInspect);
        return new ResponseResult(200, "ok", null);
    }

    //添加手术
    @PostMapping("/inpatientOperation")
    public ResponseResult addCure(@RequestBody InpatientOperation inpatientOperation){
        inpatientOperationService.insertInpatientOperation(inpatientOperation);
        return new ResponseResult(200, "ok", null);
    }

    //添加药品
    @PostMapping("/inpatientDrug")
    public ResponseResult addDrug(@RequestBody InpatientDrug inpatientDrug){
        inpatientDrugService.insertInpatientDrug(inpatientDrug);
        return new ResponseResult(200, "ok", null);
    }

    //结算,增加结算单
    @PutMapping("/inpatientOrder/{inpatientId}")
    public ResponseResult addOrder(@PathVariable int inpatientId){
        ResponseResult responseResult = new ResponseResult<>(200, "ok", inpatientService.insertOrder(inpatientId));
        return responseResult;
    }

    //结算完成
    @DeleteMapping("/inpatientOrder/{inpatientOrderId}/{inpatientId}")
    public ResponseResult closingOrder(@PathVariable int inpatientOrderId, @PathVariable int inpatientId){
        inpatientService.updateOrderState(inpatientOrderId);
        inpatientService.deleteInpatient(inpatientId);
        return new ResponseResult<>(200, "ok", null);
    }
}
